//
//  InfoViewController.m
//  OtakuWorld
//
//  Created by iOS Two on 14-9-23.
//  Copyright (c) 2014年 iOS Two. All rights reserved.
//

#import "InfoViewController.h"
#import "UIImageView+MJWebCache.h"
#import "JDStatusBarNotification.h"

@interface InfoViewController ()

@end

@implementation InfoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [JDStatusBarNotification addStyleNamed:@"SBStyle1"
                                              prepare:^JDStatusBarStyle *(JDStatusBarStyle *style) {
                                                  style.barColor = [UIColor colorWithRed:0.58 green:0.58 blue:0.58 alpha:1];
                                                  style.textColor = [UIColor whiteColor];
                                                  style.animationType = JDStatusBarAnimationTypeFade;
                                                  style.font = [UIFont fontWithName:@"SnellRoundhand-Bold" size:15.0];
                                                  style.progressBarColor = [UIColor colorWithRed:0.986 green:0.062 blue:0.598 alpha:1.000];
                                                  style.progressBarHeight = 20.0;
                                                  return style;
                                              }];
        // Custom initialization
    }
    return self;
}
-(void)viewWillDisappear:(BOOL)animated {
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [appDelegate.leveyTabBarController hidesTabBar:NO animated:YES];
    [MobClick endLogPageView:@"InfoViewController"];

}
-(void)viewWillAppear:(BOOL)animated {
    [MobClick beginLogPageView:@"InfoViewController"];
}

-(void)back {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [super.navigationController setNavigationBarHidden:NO animated:TRUE];
    [super.navigationController setToolbarHidden:YES animated:TRUE];
    UIBarButtonItem *back = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"backAR"] style:UIBarButtonItemStyleDone target:self action:@selector(back)];
    [self.navigationController.navigationBar setTintColor:[UIColor colorWithRed:0.99 green:0.62 blue:0.27 alpha:1]];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSpacer.width = -15;
    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer, back, nil];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 20)];
    label.text = @"详细信息";
    label.textColor = [UIColor colorWithRed:0.99 green:0.62 blue:0.27 alpha:1];
    label.font = [UIFont fontWithName:@"STXIHEI" size:20];
    [label sizeToFit];
    self.navigationItem.titleView = label;
    
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [appDelegate.leveyTabBarController hidesTabBar:YES animated:YES];
    
    [self getInfo];
    // Do any additional setup after loading the view.
}
-(void)getInfo {
    [self showIndeterminateProgress];
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://115.29.243.47/cartoonWorld/interface.php/news/%@?fetchSize=20",_Id]];
    ASIHTTPRequest *request = [[ASIHTTPRequest alloc]initWithURL:url];
    request.delegate = self;
    request.username = @"info";
    [request startSynchronous];
}
-(void)requestFinished:(ASIHTTPRequest *)request {
    if ([request.username isEqualToString:@"info"]) {
        _heightArray = [[NSMutableArray alloc]init];
        _widthArray = [[NSMutableArray alloc]init];
        _contextArray = [[NSMutableArray alloc]init];
        _imgArray = [[NSMutableArray alloc]init];
        SBJsonParser *jsonP = [[SBJsonParser alloc]init];
        NSArray *array =[[jsonP objectWithData:[request responseData]]objectForKey:@"details"];
        for (int i = 0; i < array.count; i++) {
            [_heightArray addObject:[[array objectAtIndex:i] objectForKey:@"height"]];
            [_widthArray addObject:[[array objectAtIndex:i] objectForKey:@"width"]];
            [_imgArray addObject:[[array objectAtIndex:i] objectForKey:@"pic"]];
            [_contextArray addObject:[[array objectAtIndex:i] objectForKey:@"text"]];
        }
        [self creatController];
    } else {
        UIImage *image = [UIImage imageWithData:[request responseData]];
        UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:),nil);
    }
    [HUD hide:YES];
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    NSString *msg = nil;
    if(error != NULL) {
        msg = @"保存图片失败";
    } else {
        msg = @"保存图片成功";
    }
    [JDStatusBarNotification showWithStatus:msg dismissAfter:2.0 styleName:@"SBStyle1"];
}

-(void)requestFailed:(ASIHTTPRequest *)request {
    NSLog(@"请求失败");
}

-(void)creatController {
    UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, 320, kHeight - 64)];
    scroll.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:scroll];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 100)];
    titleLabel.text = _titleLabel;
    titleLabel.font = [UIFont fontWithName:@"STXIHEI" size:20];
    titleLabel.numberOfLines = 0;
    [titleLabel sizeToFit];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor colorWithRed:0.93 green:0.49 blue:0.13 alpha:1];
    [scroll addSubview:titleLabel];
    
    UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(0, titleLabel.frame.size.height + titleLabel.frame.origin.y + 10, 320, 2)];
    line.image = [UIImage imageNamed:@"line_02"];
    [scroll addSubview:line];
    
    CGFloat height = line.frame.size.height;
    CGFloat y = line.frame.origin.y;
    UIImageView *image;
    UILabel *label;
    for (int i = 0; i < _contextArray.count - 1; i++) {
        image = [[UIImageView alloc]initWithFrame:CGRectMake(10, y + height + 10, 300, [[_heightArray objectAtIndex:i] floatValue]/ ([[_widthArray objectAtIndex:i] floatValue] / 300))];
        [image setImageURLStr:[_imgArray objectAtIndex:i] placeholder:[UIImage imageNamed:@"arrow"]];
        image.userInteractionEnabled = YES;
        image.tag = i;
        [image addGestureRecognizer:[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(buttonClick:)]];
        [scroll addSubview:image];
        
        label = [[UILabel alloc]initWithFrame:CGRectMake(10, image.frame.size.height + image.frame.origin.y + 10, 300, 100)];
        label.numberOfLines = 0;
        label.text = [NSString stringWithFormat:@"       %@",[_contextArray objectAtIndex:i]];
        label.font = [UIFont systemFontOfSize:13];
        label.textColor = [UIColor grayColor];
        [label sizeToFit];
        [scroll addSubview:label];
        
        height = label.frame.size.height;
        y = label.frame.origin.y;
    }
    scroll.contentSize = CGSizeMake(kwidth, height + y + 10);
}
-(void)buttonClick:(UILongPressGestureRecognizer *)sender {
    UIActionSheet *action = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"保存图片" otherButtonTitles:nil, nil];
    switch (sender.state) {
        case UIGestureRecognizerStateEnded:
            [action showInView:self.view];
            _url = [NSURL URLWithString:[_imgArray objectAtIndex:sender.view.tag]];
            break;
        case UIGestureRecognizerStateCancelled:
            
            break;
        case UIGestureRecognizerStateFailed:
            
            break;
        case UIGestureRecognizerStateBegan:
            
            break;
        case UIGestureRecognizerStateChanged:
            
            break;
        default:
            break;
    }
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        ASIHTTPRequest *request = [[ASIHTTPRequest alloc]initWithURL:_url];
        request.delegate = self;
        request.username = @"downLoad";
        [request startAsynchronous];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
